package infoholders;
/**
 * The Schedule class
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * A list of appointments. Manages schedule by disallowing overlapping
 * appointments.
 * 
 * @author William Hoffmann
 * 
 * @version $Revision: 1.0 $
 */
public class Schedule implements Serializable{
	/**
	 * The list of appointments
	 */
	private List<Appointment> appointments; // $codepro.audit.disable
											// variableShouldBeFinal

	/**
	 * Default constructor for a Schedule
	 */
	public Schedule() {
		appointments = new ArrayList<Appointment>();
	}

	/**
	 * Returns a specific appointment based on start time
	 * 
	 * @param startTime
	 *            The time at which the desired appointment starts
	 * @return Appointment object if time is found, null otherwise
	 */
	public Appointment searchByStartTime(Calendar startTime) {
		for (Appointment currentApp : appointments) {
			if (currentApp.getMyStartTime().equals(startTime)) {
				return currentApp;
			}
		}
		return null;
	}

	/**
	 * Gets all the appointments in a list
	 * 
	 * @return a list containing every appointment
	 */
	public List<Appointment> getAllAppointments() {
		return appointments;
	}

	/**
	 * Adds an appointment that does not conflict with current appointments
	 * 
	 * @param app
	 *            Appointment
	 * @return true if add was successful, false otherwise
	 */
	public boolean add(Appointment app) { // $codepro.audit.disable
											// booleanMethodNamingConvention
		// Search through each appointment to see if there
		// is a time conflict
		for (Appointment currentApp : appointments) {
			if (app.hasConflict(currentApp)) {
				return false;
			}
		}
		appointments.add(app);
		return true;
	}

	/**
	 * Returns the list of appointments as text
	 * 
	 * @return A newline separated list of appointment details
	 */
	public String toString() {
		final StringBuffer all = new StringBuffer("");
		for (Appointment currentApp : appointments) {
			all.append(currentApp.toString() + "\n");
		}
		return all.toString();
	}
}
